﻿using Microsoft.AspNetCore.Mvc;
using StudentBackendService.Services;
using StudentBackendService.ViewObjects;
using StudentBackendService.ViewObjects.Req;
using StudentManagerService.Controllers;
using StudentManagerService.Models;

namespace StudentBackendService.Controllers.v1
{
    [ApiController]
    [Route("api/[controller]")]
    [Produces("application/json")]
    public class StudentsController : ControllerBase, IStudentAPI
    {
        private readonly ILogger<StudentsController> _logger;
        private readonly IStudentManage _studentManager;

        public StudentsController(ILogger<StudentsController> logger, IStudentManage studentManager)
        {
            _logger = logger;
            _studentManager = studentManager;
        }

        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public Response AddOne(Student input)
        {
            if (input is null)
                throw new ArgumentNullException(nameof(input));
            if (input.Email is null)
                throw new ArgumentNullException("email", "不能为空");
            var data = _studentManager.AddOne(input);
            return ViewObjects.Response.Success(data);
        }

        [HttpGet("{id}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public Response GetById(int id)
        {
            _logger.LogInformation("find {id} Student Info", id);
            var data = _studentManager.GetById(id);
            return ViewObjects.Response.Success(data);
        }

        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public Response List(string? email, string? name, int page, int size)
        {
            StudentListReq req = new() { Email = email, Name = name, Page = page, Size = size };
            return ViewObjects.Response.Success(_studentManager.List(req));
        }
    }
}
